﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ViewOnCore.AddIn;
using ViewOnCore.Device;
using ViewOnCore.HostContract;

namespace DVDDeviceAddIn.Dvd
{
    [AddIn("DVDMediaDevice", Version = "0.5.0.0", Publisher = "ViewOn Media Services")]
    public class AddIn
    {
        public AddIn()
        {
            Host.MediaService.HardwareDeviceConnected += new HardwareDeviceEventHandler(MediaService_HardwareDeviceConnected);
            Host.MediaService.HardwareDeviceDisconnected += new HardwareDeviceEventHandler(MediaService_HardwareDeviceDisconnected);

            foreach (string drive in Directory.GetLogicalDrives())
            {
                DriveInfo info = new DriveInfo(drive);
                if (info.DriveType == DriveType.CDRom && info.IsReady)
                    Host.MediaService.AddMediaDevice(new DVDMediaDevice(drive));
            }
        }

        void MediaService_HardwareDeviceDisconnected(string driveLetter)
        {
            DriveInfo info = new DriveInfo(driveLetter);
            if (info.DriveType == DriveType.CDRom)
                foreach (MediaDevice device in Host.MediaService.MediaDevices)
                    if (device.DriveLetter.StartsWith(driveLetter))
                    {
                        Host.MediaService.RemoveMediaDevice(device);
                        return;
                    }
        }

        void MediaService_HardwareDeviceConnected(string driveLetter)
        {
            DriveInfo info = new DriveInfo(driveLetter);
            if (info.DriveType == DriveType.CDRom && info.IsReady)
                Host.MediaService.AddMediaDevice(new DVDMediaDevice(driveLetter));
        }

    }
}
